import {
	apiMixUrlApi
} from "@/api/assemb.js";
import {
	manageAppletsShare
} from "@/business/bindUser.js";
import {
	getOpenid
} from "@/business/getOpenid.js";

function dologin() {
	let baseinfo = wx.getStorageSync("baseinfo")
	uni.login({
		provider: 'weixin',
		success: function(code) {
			let param = {
				code: code.code
			};
			console.log(code.code, 'code.code', '-------------------------------------------------------');
			// uni.showLoading({
			// 	title: '登录中...',
			// 	mask: true
			// })
			apiMixUrlApi(param, 1091, "WXLogin").then(data => {
				console.log(data, '=====这是登陆的东西')
				if (data.status == 0) {
					// 存储 baseinfo
					let baseinfo = uni.getStorageSync('baseinfo');
					baseinfo.userid = data.userid;
					baseinfo.sid = data.sid;
					baseinfo.openid = data.openid;
					baseinfo.deviceid = data.openid;
					uni.setStorageSync('baseinfo', baseinfo);
					vipType(false);
					if (data.isFirstLogin) {
						// 邀请记录
						let inviterId = wx.getStorageSync("inviterId");
						console.log('inviterId>>', inviterId);
						if (inviterId) {
							manageAppletsShare(inviterId);
						}
					}
					if (uni.getStorageSync('ban')) {
						uni.removeStorageSync('ban')
					}
					apiMixUrlApi({}, 1042, 'GetPaymentInfo').then(data => {
						console.log(data,'拿次数的地方')
						if (data.descinfo == "success") {
							uni.setStorageSync('frequency', data.coins)
						}
					});
					// uni.showToast({
					// 	title: '登录成功',
					// 	icon: "success",
					// 	mask: true,
					// 	duration: 1000
					// })
				} else if (data.status == 100015) {
					uni.setStorageSync("ban", true);
					uni.setStorageSync("statement", true);
					// uni.showModal({
					// 	title: '提示',
					// 	content: '账号违规，请联系客服',
					// 	showCancel: true,
					// 	success: function(res) {
					// 		if (res.confirm) {
					// 			console.log('用户点击确定');
					// 		}
					// 	}
					// });
					getOpenid().then((res) => {
						let par = {
							openid: res
						}
						apiMixUrlApi(par, "1105", "GetBanUseridByOpenid").then((json) => {
							let baseinfo = uni.getStorageSync('baseinfo');
							baseinfo.userid = json.userid;
							uni.setStorageSync('baseinfo', baseinfo);
						});
					});
				} else {
					// uni.showToast({
					// 	title:data.descinfo,
					// 	icon:'none',
					// 	duration:4000
					// })
					uni.showModal({
						title: '提示',
						content: data.descinfo,
						showCancel: true,
						success: function(res) {
							if (res.confirm) {
								console.log('用户点击确定');
							}
						}
					});
				}
			});
			uni.getUserProfile({
				desc: "需要获取用于完善登录信息",
				success: function(code) {
					uni.setStorageSync('UserProfile', code.userInfo);
				},
				fail(res) {}
			});
		}
	});

	// UserProfile();
}

function UserProfile() {
	uni.showModal({
		title: '温馨提示',
		content: '需要获取您的微信昵称与头像用于完善登录信息，是否同意？',
		success: function(res) {
			if (res.confirm) {
				console.log('用户点击确定');
				uni.getUserProfile({
					desc: "需要获取用于完善登录信息",
					success: function(code) {
						uni.setStorageSync('UserProfile', code.userInfo);
					},
					fail(res) {}
				});
			} else if (res.cancel) {
				console.log('用户点击取消');
			}
		}
	});
}

function calculateVipTime(time) {
	let viptime = time.split(' ')[0];
	let year = viptime.split('-')[0];
	var timestamp = Date.parse(new Date());
	var date = new Date(timestamp);
	var yearNow = date.getFullYear();
	let yearTime = year - yearNow;
	console.log(yearTime, "yearTime");
	if (yearTime >= 30) {
		return '永久会员';
	} else {
		return time.slice(0, 11);
	}
}

function vipType(isJump) {
	apiMixUrlApi({}, 1042, 'GetPaymentInfo').then(data => {
		console.log(data, "data");
		if (data.descinfo == "success") {
			if (data.vipendtime) {
				let viptime = calculateVipTime(data.vipendtime);
				console.log(viptime, "viptime");
				let vipType = {
					vipStatus: data.vipStatus,
					vipendtime: viptime
				};
				uni.setStorageSync('vipType', vipType);
			}
			if (isJump) {
				uni.navigateBack({
					delta: 1
				});
			}
		}
	});
	// let vipType = {
	// 	vipStatus: 1,
	// 	vipendtime: '测试会员'
	// };
	// uni.setStorageSync('vipType', vipType);
}

function isLogin() {
	return !!uni.getStorageSync('baseinfo').openid ? true : false;
}
/**
 * 判断是否永久会员
 * @param {string} time 会员时间
 * @param {string} type 会员类型（1:vip 2:svip）
 */


export {
	dologin,
	isLogin,
	UserProfile,
	vipType
};